package rsschart.charter;

import java.util.Iterator;

import rsschart.feed.DataSet;
import rsschart.feed.DataSets;
import rsschart.feed.RssChart;

public abstract class LineBarCharter {

	protected final int LBL_ON_AXIS = 0;
	protected final int LBL_BETWEEN_AXIS = 1;
	

	
	
	protected double getMaxValue(DataSets dataSets) {
		String[] vals;
		//-- to do: make a large negative number...
		Double maxVal=0.0;
		Double currVal;
		Iterator setIter;
		setIter = dataSets.iterator();
		DataSet dataSet;
		int setSize;
		
		//-- iterate through
		while(setIter.hasNext()) {
			dataSet = (DataSet)setIter.next();
			vals = RssChart.parseValues(dataSet.getValueString());
			setSize = vals.length;
			for(int i=0;i<setSize;i++) {
				currVal = Double.parseDouble(vals[i]);
				maxVal = currVal > maxVal ? currVal : maxVal;
			}
		}
		return maxVal;
	}

	protected double getMinValue(DataSets dataSets) {
		String[] vals;
		//-- to do: make a large negative number...
		Double minVal=0.0;
		Double currVal;
		Iterator setIter;
		setIter = dataSets.iterator();
		DataSet dataSet;
		int setSize;
		
		//-- iterate through
		while(setIter.hasNext()) {
			dataSet = (DataSet)setIter.next();
			vals = RssChart.parseValues(dataSet.getValueString());
			setSize = vals.length;
			for(int i=0;i<setSize;i++) {
				currVal = Double.parseDouble(vals[i]);
				minVal = currVal < minVal ? currVal : minVal;
			}
		}

		return minVal;
	}

	/**
	 * This method returns the increment between tick marks in graph values, 
	 * not SVG view space increments. So if the maxium value is 100 and the minimum value
	 * is 0, this method would return 20 (b/c is the highest divisor and 100 mod 20 is zero...)
	 * @param maxVal
	 * @param minVal
	 * @return
	 */
	protected double getTickIncVal(double maxVal, double minVal) {
		double tickMax = Math.abs(minVal)>maxVal?Math.abs(minVal):maxVal;
		int tickInc=1;
		
		for (int div=5; div>0;div--) {
			if (tickMax%div==0) {
				tickInc = (int)tickMax/div;
				break;
			}
		}
		return tickInc;
	}
	
	protected String drawTickMark(double val, double yLoc) {
		StringBuffer  tick = new StringBuffer();
		tick.append("<line x1=\"50\" y1=\"" + yLoc + "\" x2=\"52\" y2=\"" + yLoc + "\" fill=\"none\" stroke=\"black\" stroke-width=\"0.1px\"/>\n");
		tick.append("<text x=\"45\" y=\"" + (yLoc+2)+ "\" text-anchor=\"end\" font-size=\"8\">" + Double.toString(val) + "</text>\n");
		
		return tick.toString();
	}

	protected String drawCatgLabel(double midX, double midY, String label) {
		String catgLabel;
		catgLabel = "<text x=\"" + midX + "\" y=\"" + midY + "\" text-anchor=\"middle\" font-size=\"8\">" +
				label + "</text>\n";
		return catgLabel;
	}

	
	protected String drawYAxisLabel(double midX, double midY, String label) {
		String text;
		text = "<text x=\"0\" y=\"0\" text-anchor=\"middle\" font-size=\"8\" transform=\"translate(" + 
				midX + "," + midY + "),rotate(-90)\">" + label + "</text>";
		return text;

	}
	
}
